package com.example.project.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.project.dto.vo.PageResult;
import com.example.project.dto.vo.admin.AdminRoleVO;
import com.example.project.dto.vo.admin.AdminUserVO;
import com.example.project.entity.AdminUserRole;
import com.example.project.entity.Role;
import com.example.project.entity.User;
import com.example.project.mapper.AdminUserRoleMapper;
import com.example.project.mapper.RoleMapper;
import com.example.project.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@RequiredArgsConstructor
public class AdminUserService {

    private final UserMapper userMapper;
    private final RoleMapper roleMapper;
    private final AdminUserRoleMapper adminUserRoleMapper;

    public PageResult<AdminUserVO> listUsers(int page, int size) {
        Page<User> p = userMapper.selectPage(Page.of(page, size), new LambdaQueryWrapper<User>());
        List<AdminUserVO> vos = new ArrayList<>();
        for (User u : p.getRecords()) {
            AdminUserVO vo = new AdminUserVO();
            vo.setId(u.getId().intValue());
            vo.setUserName(u.getUserName());
            vo.setEmail(u.getEmail());
            vo.setAvatar(u.getAvatar());
            vo.setState(u.getState() != null && u.getState() == 1);
            // 角色
            List<AdminUserRole> binds = adminUserRoleMapper.selectList(new LambdaQueryWrapper<AdminUserRole>().eq(AdminUserRole::getUid, u.getId().intValue()));
            if (binds != null && !binds.isEmpty()) {
                List<Integer> rids = new ArrayList<>();
                for (AdminUserRole b : binds) { rids.add(b.getRid()); }
                List<Role> roles = rids.isEmpty() ? Collections.emptyList() : roleMapper.selectBatchIds(rids);
                List<AdminRoleVO> roleVos = new ArrayList<>();
                for (Role r : roles) {
                    AdminRoleVO rv = new AdminRoleVO();
                    rv.setId(r.getId());
                    rv.setName(r.getName());
                    rv.setNameZh(r.getNameZh());
                    rv.setEnabled(Boolean.TRUE.equals(r.getEnabled()));
                    rv.setPerms(Collections.emptyList());
                    rv.setMenus(Collections.emptyList());
                    roleVos.add(rv);
                }
                vo.setRoles(roleVos);
            } else {
                vo.setRoles(Collections.emptyList());
            }
            vos.add(vo);
        }
        return new PageResult<>(vos, p.getTotal());
    }

    public void updateState(Integer id, boolean state) {
        User u = new User();
        u.setId(id.longValue());
        u.setState(state ? 1 : 0);
        userMapper.updateById(u);
    }

    @Transactional
    public void updateUser(Integer id, AdminUserVO body) {
        User u = new User();
        u.setId(id.longValue());
        u.setEmail(body.getEmail());
        userMapper.updateById(u);

        adminUserRoleMapper.delete(new LambdaQueryWrapper<AdminUserRole>().eq(AdminUserRole::getUid, id));
        if (body.getRoles() != null) {
            for (AdminRoleVO rv : body.getRoles()) {
                AdminUserRole bind = new AdminUserRole();
                bind.setUid(id);
                bind.setRid(rv.getId());
                adminUserRoleMapper.insert(bind);
            }
        }
    }

    public void resetPassword(Integer id) {
        User u = new User();
        u.setId(id.longValue());
        u.setPassword("123");
        userMapper.updateById(u);
    }
}


